﻿using System;
using System.Text.RegularExpressions;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using EmptyWallGallery.Core;
using EmptyWallGallery.Core.Utilities;

namespace EmptyWallGallery.FrontEnd
{
    public partial class ArtistsDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindData();
            BindData(Convert.ToInt32(Page.RouteData.Values["AlbumId"]), 0);
        }

        public void BindData(int albumId, int status)
        {
            lstPhoto.DataSource = Core.BLL.Images.Instance.GetByAlbum(status, albumId, true);
            lstPhoto.DataBind();
        }

        public void BindData()
        {
            if (Page.RouteData.Values["AlbumId"] == null)
            {
                Response.Redirect(Configs.HomeUrl + "Error.aspx?e=404");
            }
            else
            {
                var objAlbum = Core.BLL.Album.Instance.GetById(Convert.ToInt32(Page.RouteData.Values["AlbumId"]));

                if (objAlbum == null) return;

                titleArtist.InnerHtml = objAlbum.AlbumName;
                title.InnerHtml = "Biography";
                body.InnerHtml = Server.HtmlDecode(objAlbum.AlbumDesc);
                //PostInfo.InnerHtml = "Đăng vào: " +
                //                         Convert.ToDateTime(objArticle.ReleaseDate).AddHours(Configs.TimeLine).ToString(
                //                             "dd-MM-yyyy HH:mm tt");

                Page.Title = objAlbum.AlbumName + " - " + Configs.WebTitle;
                Page.MetaDescription = objAlbum.AlbumName + " - " + Configs.WebDescription;

                //Update View
                Core.BLL.Album.Instance.UpdateViewCount(objAlbum.AlbumId);
            }
        }

        protected string WriteImage(string fileName, DateTime createDate, string uploadBy)
        {
            var objUser = Membership.GetUser(Guid.Parse(uploadBy));
            if (objUser == null) return "";

            var strImageDefault = fileName == ""
                                      ? "/images/no_image_s.gif"
                                      : "/Uploads/" + objUser.UserName + "/" + createDate.Year + "/" +
                                        Common.Instance.Right("0" + createDate.Month, 2) + "/" +
                                        Common.Instance.Right("0" + createDate.Day, 2) + "/";

            return "<a href=\"" + strImageDefault + "700x0__" + fileName + "\" rel=\"TopProduct\"><img src=\"" +
                   strImageDefault + "500x0__" + fileName + "\" border=\"0\" width=\"180\" /></a>";
        }

        protected void DtPgArticlePreRender(object sender, EventArgs e)
        {
            foreach (Control control in dtPgArticle.Controls)
            {
                foreach (Control c in control.Controls)
                {
                    var currentLink = c as HyperLink;
                    if (currentLink == null) continue;

                    //temp = currentLink.Text;
                    //currentLink.Text = "<span>" + temp + "</span>";
                    var match = Regex.Match(currentLink.NavigateUrl, "Page=(?<Page123>.*?).*");

                    if (!match.Success) continue;

                    if (Request.QueryString["Page"] == null)
                    {
                        currentLink.NavigateUrl = Request.RawUrl + "?" + match.Value;
                    }
                    else
                    {
                        currentLink.NavigateUrl =
                            Request.RawUrl.Replace("Page=" + Request.QueryString["Page"], "") + match.Value;
                    }
                }
            }
        }
    }
}